package com.example.demo.PyTest;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.PyTest.entity.SectionEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

public class JavaCallPython {

    public static void main(String[] args) {
//        String a = "{\"3.77\": 7182, \"3.91\": 12754, \"4.04\": 19766, \"3.84\": 9737, \"3.85\": 10281, \"3.81\": 8728, \"3.74\": 6151, \"3.68\": 4481, \"3.72\": 5575, \"3.83\": 9416, \"3.97\": 15562, \"4.02\": 18307, \"3.93\": 13609, \"3.76\": 6974, \"3.82\": 9007, \"3.89\": 11959, \"3.94\": 13981, \"3.9\": 12235, \"3.73\": 5925, \"3.79\": 7842, \"3.71\": 5248, \"3.62\": 3312, \"3.98\": 16033, \"4.0\": 17365, \"3.92\": 13142, \"3.86\": 10683, \"3.8\": 8437, \"3.78\": 7667, \"3.88\": 11456, \"3.7\": 4979, \"3.63\": 3397, \"3.59\": 2861, \"3.6\": 3074, \"3.64\": 3756, \"3.66\": 4009, \"3.46\": 2280, \"3.99\": 16651, \"4.08\": 22364, \"4.06\": 20767, \"3.75\": 6553, \"3.69\": 4613, \"3.58\": 2798, \"3.38\": 2197, \"3.36\": 2195, \"3.43\": 2208, \"4.12\": 25523, \"4.18\": 30815, \"4.16\": 28896, \"3.67\": 4123, \"3.65\": 3721, \"3.87\": 11178, \"3.96\": 15208, \"4.01\": 17668, \"3.55\": 2630, \"3.4\": 2183, \"3.35\": 2164, \"3.34\": 2272, \"3.3\": 2223, \"3.31\": 2201, \"4.13\": 26480, \"4.03\": 19065, \"4.22\": 34390, \"4.29\": 41170, \"4.3\": 42570, \"4.25\": 37758, \"4.07\": 21479, \"3.61\": 3154, \"3.48\": 2334, \"3.26\": 2297, \"3.27\": 2174, \"3.42\": 2175, \"4.26\": 38721, \"4.24\": 36959, \"4.17\": 30079, \"4.11\": 24691, \"4.15\": 27741, \"4.19\": 31587, \"4.34\": 45059, \"4.32\": 43652, \"4.21\": 33594, \"4.14\": 26934, \"3.57\": 2762, \"3.5\": 2328, \"3.41\": 2202, \"3.37\": 2227, \"3.32\": 2233, \"3.44\": 2212, \"3.49\": 2375, \"4.09\": 23061, \"4.23\": 35829, \"4.27\": 40109, \"4.28\": 40140, \"4.38\": 47304, \"4.46\": 48939, \"4.44\": 48677, \"4.35\": 45695, \"4.05\": 20308, \"3.52\": 2399, \"3.53\": 2490, \"3.51\": 2450, \"4.1\": 23750, \"3.95\": 14865, \"4.4\": 47915, \"4.54\": 46942, \"4.58\": 46052, \"4.5\": 48682, \"3.25\": 2162, \"3.22\": 2278, \"3.17\": 2360, \"3.24\": 2350, \"3.47\": 2272, \"4.56\": 46244, \"4.66\": 45351, \"4.62\": 44848, \"4.49\": 48363, \"4.33\": 44535, \"3.28\": 2139, \"3.2\": 2349, \"3.14\": 2339, \"4.63\": 45010, \"4.61\": 45567, \"3.45\": 2163, \"3.21\": 2328, \"3.29\": 2147, \"3.56\": 2676, \"4.36\": 46605, \"4.31\": 43325, \"3.54\": 2515, \"4.2\": 32618, \"3.39\": 2061, \"3.33\": 2203, \"3.18\": 2289, \"3.19\": 2365, \"3.23\": 2244, \"3.02\": 2253, \"3.06\": 2453, \"3.13\": 2355, \"3.01\": 2367, \"4.41\": 48465, \"4.37\": 46961, \"3.03\": 2345, \"3.15\": 2336, \"3.1\": 2405, \"4.39\": 48356, \"4.86\": 55371, \"4.89\": 57992, \"4.9\": 59013, \"4.94\": 62217, \"5.03\": 66852, \"5.08\": 66299, \"5.06\": 66723, \"5.02\": 66976, \"5.0\": 65885, \"4.97\": 64736, \"4.91\": 60213, \"4.83\": 53515, \"4.77\": 49283, \"4.8\": 50884, \"4.95\": 63290, \"5.01\": 65931, \"4.98\": 64597, \"5.05\": 66662, \"5.1\": 66375, \"4.79\": 49924, \"4.84\": 54174, \"4.87\": 56463, \"4.85\": 54950, \"4.96\": 63317, \"5.09\": 66815, \"4.99\": 65455, \"4.78\": 49730, \"4.7\": 45946, \"4.74\": 47676, \"4.93\": 61404, \"4.48\": 48752, \"4.47\": 48534, \"4.92\": 60274, \"4.81\": 52044, \"4.76\": 48209, \"4.88\": 56895, \"5.13\": 64574, \"5.04\": 66765, \"4.75\": 47430, \"4.43\": 48926, \"4.42\": 48787, \"5.16\": 61102, \"5.2\": 56875, \"5.18\": 59161, \"5.11\": 65535, \"5.19\": 58569, \"5.07\": 66541, \"5.23\": 52315, \"5.22\": 53572, \"5.24\": 50389, \"5.26\": 46534, \"5.29\": 40968, \"5.3\": 38248, \"5.27\": 44592, \"5.32\": 34266, \"5.25\": 48940, \"5.12\": 64932, \"4.67\": 45225, \"4.6\": 45705, \"4.51\": 47604, \"4.82\": 52607, \"5.14\": 63806, \"5.17\": 60584, \"4.68\": 45804, \"4.57\": 46441, \"4.55\": 46827, \"4.53\": 47449, \"5.15\": 62319, \"5.21\": 55543, \"4.71\": 46404, \"4.73\": 47055, \"4.72\": 46622, \"4.69\": 45854, \"4.64\": 45129, \"4.65\": 44992, \"4.45\": 48733, \"4.59\": 45618, \"4.52\": 47854, \"5.33\": 32094, \"5.31\": 36168, \"5.34\": 29792, \"5.38\": 21750, \"5.41\": 16326, \"5.28\": 42814, \"5.35\": 27598, \"5.37\": 23477, \"5.44\": 11844, \"5.43\": 13367, \"5.46\": 9321, \"5.5\": 5505, \"5.52\": 4342, \"5.4\": 17935, \"5.39\": 19807, \"5.42\": 14707, \"5.45\": 10714, \"5.48\": 7361, \"5.51\": 4964, \"5.53\": 3605, \"5.47\": 8431, \"5.36\": 25917, \"5.49\": 6337, \"5.57\": 1911, \"5.55\": 2659, \"5.54\": 3224, \"5.56\": 2271, \"5.59\": 1435, \"5.58\": 1647, \"3.11\": 2421, \"3.09\": 2372, \"2.95\": 2105, \"2.94\": 2047, \"3.0\": 2236, \"2.93\": 2030, \"2.98\": 2161, \"2.91\": 1863, \"2.99\": 2231, \"3.08\": 2379, \"5.6\": 1209, \"5.61\": 1010, \"2.96\": 2028, \"2.97\": 2151, \"3.04\": 2283, \"3.05\": 2334, \"2.92\": 2038, \"2.9\": 1878, \"3.12\": 2366, \"3.16\": 2425, \"3.07\": 2401, \"2.88\": 1743, \"2.74\": 953, \"2.87\": 1661, \"2.7\": 674, \"2.86\": 1569, \"2.82\": 1434, \"2.85\": 1588, \"2.78\": 1156, \"2.67\": 670, \"2.76\": 1003, \"2.89\": 1780, \"2.79\": 1253, \"2.77\": 1151, \"2.83\": 1509, \"2.75\": 969, \"2.72\": 851, \"2.84\": 1480, \"2.81\": 1419, \"2.8\": 1274, \"2.65\": 789, \"2.68\": 711, \"2.6\": 2706, \"2.69\": 711, \"2.62\": 1864, \"2.73\": 835, \"2.71\": 781, \"2.61\": 2373, \"2.63\": 1402, \"2.66\": 686, \"2.64\": 985, \"5.65\": 483, \"5.62\": 887, \"5.63\": 717, \"5.66\": 413, \"5.67\": 329, \"5.7\": 181, \"5.64\": 557, \"5.68\": 277, \"5.69\": 243, \"1.99\": 2291, \"1.98\": 2384, \"1.96\": 2304, \"2.0\": 2254, \"2.07\": 2241, \"2.14\": 2468, \"2.17\": 2610, \"2.21\": 2690, \"2.24\": 2905, \"2.16\": 2519, \"2.06\": 2190, \"2.05\": 2207, \"2.12\": 2388, \"2.15\": 2546, \"2.1\": 2321, \"2.28\": 3262, \"2.26\": 3088, \"2.27\": 3094, \"2.04\": 2234, \"2.09\": 2367, \"2.11\": 2339, \"2.08\": 2300, \"2.22\": 2808, \"2.18\": 2599, \"2.13\": 2400, \"1.95\": 2261, \"2.03\": 2237, \"1.91\": 2409, \"1.88\": 2537, \"1.86\": 2625, \"2.19\": 2650, \"2.29\": 3354, \"2.3\": 3433, \"2.2\": 2648, \"2.23\": 2757, \"2.35\": 4135, \"2.38\": 4499, \"2.36\": 4303, \"2.32\": 3815, \"2.25\": 2893, \"5.73\": 63, \"1.87\": 2568, \"2.02\": 2323, \"2.01\": 2306, \"1.94\": 2421, \"1.84\": 2739, \"1.82\": 2835, \"1.92\": 2404, \"2.33\": 3997, \"2.37\": 4383, \"2.41\": 4554, \"2.44\": 4584, \"5.76\": 36, \"2.34\": 4175, \"2.39\": 4554, \"2.31\": 3723, \"2.4\": 4582, \"2.42\": 4624, \"2.45\": 4521, \"2.47\": 4493, \"2.46\": 4528, \"2.43\": 4642, \"5.75\": 41, \"1.85\": 2658, \"1.8\": 2968, \"1.77\": 3230, \"1.76\": 3324, \"1.78\": 3162, \"1.81\": 2895, \"1.89\": 2435, \"1.66\": 3229, \"1.65\": 3146, \"1.64\": 3187, \"1.63\": 3050, \"1.62\": 2937, \"1.68\": 3382, \"1.71\": 3604, \"1.73\": 3559, \"1.72\": 3545, \"1.7\": 3505, \"1.74\": 3420, \"1.75\": 3340, \"1.69\": 3519, \"1.67\": 3400, \"1.61\": 2922, \"1.56\": 2397, \"1.52\": 2051, \"1.54\": 2314, \"1.6\": 2826, \"1.79\": 3158, \"1.83\": 2852, \"1.93\": 2379, \"1.9\": 2435, \"2.5\": 4424, \"2.48\": 4513, \"2.49\": 4475, \"1.59\": 2788, \"1.97\": 2242, \"1.57\": 2575, \"1.58\": 2570, \"2.52\": 4091, \"2.51\": 4229, \"2.53\": 4050, \"2.54\": 3831, \"2.55\": 3650, \"2.58\": 2911, \"2.57\": 3248, \"2.56\": 3424, \"2.59\": 2724, \"5.71\": 111, \"1.53\": 2146, \"1.55\": 2425, \"1.51\": 2024, \"5.77\": 14, \"5.74\": 42, \"1.47\": 2030, \"1.43\": 2398, \"1.42\": 2584, \"1.39\": 2549, \"1.37\": 2066, \"1.38\": 2309, \"1.44\": 2319, \"1.41\": 2597, \"1.36\": 1722, \"1.35\": 1400, \"1.34\": 1115, \"1.33\": 1008, \"1.31\": 767, \"1.48\": 1922, \"1.4\": 2626, \"1.46\": 2098, \"1.32\": 900, \"1.49\": 1954, \"1.45\": 2215, \"1.5\": 1859, \"1.3\": 391, \"5.72\": 101, \"5.78\": 11, \"5.79\": 4, \"5.82\": 1}";
        String pythonScriptPath = "E:\\BackEndCode\\E_MyUtil\\src\\main\\resources\\static\\PyFile\\spectrogram-data-analysis.py";
        String arg1 = "E:\\BackEndCode\\E_MyUtil\\src\\main\\resources\\static\\PyFile\\LAI.tif";
        ProcessBuilder pb = new ProcessBuilder("python", pythonScriptPath, arg1);
        pb.redirectErrorStream(true);
        try {
            Process p = pb.start();
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = in.readLine();
            if (line != null) {
//                System.out.println(line);
                System.out.println("-------根据固定区间分析数据-------");
                fixedIntervalAnalysisData(line);
                System.out.println("-------根据分段数分析数据-------");
                analyzeNumberOfSegmentsData(line, 4);
                System.out.println("-------根据指定区间分析数据-------");
                specifiedIntervalAnalysisData(line);

            }
            in.close();
            p.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据指定区间分析数据
     *
     * @param line 数据源
     */
    static void specifiedIntervalAnalysisData(String line) {
        // 区间数据
        List<SectionEntity> sectionList = Arrays.asList(
                new SectionEntity(null, 0.01, "#ffffff"),
                new SectionEntity(0.01, 0.5, "#f47a35"),
                new SectionEntity(0.5, 1.0, "#f7a141"),
                new SectionEntity(1.0, 1.5, "#fccd4e"),
                new SectionEntity(1.5, 2.0, "#fdfa5c"),
                new SectionEntity(2.0, 2.5, "#bcf85c"),
                new SectionEntity(2.5, 3.0, "#7fe661"),
                new SectionEntity(3.0, 3.5, "#54c650"),
                new SectionEntity(3.5, 4.0, "#33914d"),
                new SectionEntity(4.0, 4.5, "#7feafa"),
                new SectionEntity(4.5, 5.0, "#3dccf7"),
                new SectionEntity(5.0, 5.5, "#3fa1f3"),
                new SectionEntity(5.5, 6.0, "#295eef"),
                new SectionEntity(6.0, 6.5, "#002fed"),
                new SectionEntity(6.5, 7.0, "#6033ed"),
                new SectionEntity(7.0, 7.5, "#a439ee"),
                new SectionEntity(7.5, null, "#ef42f0"));

        JSONObject jsonObject = JSONObject.parseObject(line);
        // 获取总值
        long allSum = jsonObject.values().stream().mapToLong(o -> Long.parseLong(o.toString())).sum();
        System.out.println("所有值的总和:" + allSum);

        // 遍历原始 JSON 对象的每个键值对
        for (SectionEntity sectionEntity : sectionList) {
            long sectionSum = 0;
            for (String key : jsonObject.keySet()) {
                if (sectionEntity.isInSection(Double.parseDouble(key))) {
                    sectionSum += jsonObject.getLong(key);
                }
            }
            sectionEntity.setIntervalProportion(Math.round(((double) sectionSum / allSum) * 10000) / 100.0);
            if (sectionSum > 0) {
                System.out.println("区间：" + sectionEntity.getIntervalLower() + " - " + sectionEntity.getIntervalUpper() + "，总和：" + sectionSum + ",占总比的 " + sectionEntity.getIntervalProportion() + "%,色值为:" + sectionEntity.getColorValue());
            }
        }
        System.out.println(sectionList.stream().filter(x -> x.getIntervalProportion() > 0).collect(Collectors.toList()));
    }

    /**
     * 根据分段数分析数据
     *
     * @param line          数据源
     * @param intervalCount 最大区间量
     */
    static void analyzeNumberOfSegmentsData(String line, int intervalCount) {
        // 区间数据
        List<SectionEntity> sectionList = new ArrayList<>(4);

        JSONObject jsonObject = JSONObject.parseObject(line);
        double max = jsonObject.entrySet().stream().collect(Collectors.summarizingDouble(k -> Double.parseDouble(k.getKey()))).getMax();
        double min = jsonObject.entrySet().stream().collect(Collectors.summarizingDouble(k -> Double.parseDouble(k.getKey()))).getMin();
        System.out.println("最大值：" + max + ",最小值：" + min);

        // 设置区间参数
        double interval = (max - min) / intervalCount;
        for (int i = 0; i < intervalCount; i++) {
            double low = Math.round((min + interval * i) * 100) / 100.0;
            double upper = Math.round((min + interval * (i + 1)) * 100) / 100.0;
            // 避免因为精度导致数据丢失
            if (i == 0) {
                low -= 0.1;
            }
            if (i == intervalCount - 1) {
                upper += 0.1;
            }
            SectionEntity sectionEntity = new SectionEntity(low, upper, null);
            sectionList.add(sectionEntity);
        }

        // 获取总值
        long allSum = jsonObject.values().stream().mapToLong(o -> Long.parseLong(o.toString())).sum();
        System.out.println("所有值的总和:" + allSum);

        // 遍历原始 JSON 对象的每个键值对
        for (SectionEntity sectionEntity : sectionList) {
            long sectionSum = 0;
            for (String key : jsonObject.keySet()) {
                if (sectionEntity.isInSection(Double.parseDouble(key))) {
                    sectionSum += jsonObject.getLong(key);
                }
            }
            sectionEntity.setIntervalProportion(Math.round(((double) sectionSum / allSum) * 10000) / 100.0);
            System.out.println("区间：" + sectionEntity.getIntervalLower() + " - " + sectionEntity.getIntervalUpper() + "，总和：" + sectionSum + ",占总比的 " + sectionEntity.getIntervalProportion() + "%");
        }
    }

    /**
     * 根据固定区间分析数据
     *
     * @param line 数据源
     */
    static void fixedIntervalAnalysisData(String line) {
        JSONObject jsonObject = JSONObject.parseObject(line);

        Long allSum = jsonObject.values().stream().mapToLong(o -> Long.parseLong(o.toString())).sum();
        System.out.println("所有值的总和:" + allSum);
        // 创建 TreeMap 存储区间分组数据
        TreeMap<Double, Long> intervalSumMap = new TreeMap<>();

        // 遍历原始 JSON 对象的每个键值对
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            // 获取键和值
            String key = entry.getKey();
            Long value = Long.parseLong(entry.getValue().toString());

            // 将键转换为对应的区间
            double intervalKey = Math.floor(Double.parseDouble(key) / 0.5) * 0.5;

            // 将值累加到该区间对应的总和中
            Long sum = intervalSumMap.getOrDefault(intervalKey, 0L);
            intervalSumMap.put(intervalKey, sum + value);
        }

        // 打印每个区间对应的总和
        for (Map.Entry<Double, Long> entry : intervalSumMap.entrySet()) {
            System.out.println("区间：" + entry.getKey() + " - " + (entry.getKey() + 0.5) + "，总和：" + entry.getValue() + ",占总比的 " + Math.round(((double) entry.getValue() / allSum) * 10000) / 100.0 + "%");
        }
    }

}
